<template>
	<view class="container">
		<image class="logo" src="https://jsgl.zfcxjw.cq.gov.cn:28082/prod-api/profile/upload/2025/06/17/logo_20250617105634A984.png" ></image>
		<text class="title">重庆市住房和城乡建设工程质量安全服务平台</text>
		<button v-if="!checked" class="login-btn" @click="empower">微信一键登录</button>
		<button v-else class="login-btn" open-type="getPhoneNumber" @getphonenumber="getUserPhoneNumber">微信一键登录</button>
		<!-- <view class="policy" @click="checked = !checked">
			<view class="out-circle" :class="checked ? 'active' : ''">
			<view class="inner-c" :class="checked ? 'i-active' : ''"></view>
			</view>
			<view class="label">
			我已阅读并同意<text @click.prevent="agreement('user')">《用户协议》</text>及<text @click.prevent="agreement('privacy')">《隐私政策》</text>
			</view>
		</view> -->
		<text class="footer">重庆市住房和城乡建设委员会质安总站</text>
	</view>
</template>

<script setup>
import { ref } from 'vue';
import { onLoad } from "@dcloudio/uni-app";
import { Login } from '@/api/qss/index.js'
import { getUserInfo } from '@/utils/permission.js'

const checked = ref(true)
function empower(){
    uni.showToast({
      icon: 'none',
      title: '请先阅读并同意《用户服务协议》和《隐私协议》'
    })
  }


function getUserPhoneNumber(event) {
	if(event.detail.errMsg != "getPhoneNumber:ok") {
		// 授权失败
		uni.showToast({
			icon: 'none',
			title: '请先授权手机号'
		})
		return false;
	}
	// 发起请求，进行授权登录
	console.log(event);
	uni.login({
		provider: 'weixin',
		success: (res) => {
			if (res.errMsg = 'login:ok') {
				uni.showLoading({ title: '登录中...' })
				Login({
					code: event.detail.code,
					jsCode: res.code
				}).then((res)=>{
					if(res.code == 200){
						uni.setStorageSync('phone', res.data.phone)
						uni.setStorageSync('token', res.data.token)
						/* 获取用户信息 */
						getUserInfo().then((res)=>{
							console.log(res,'res11111')
						    // 跳转首页
							if (res.code === 200 && res.data) {
								uni.switchTab({
									url: '/pages/index/index'
								})
							}
							// uni.navigateTo({
							// 	url: '/pagesC/certification/realName/index'
							// })
								
						}).catch((err)=>{
						    // 去认证
							uni.navigateTo({
								url: '/pagesC/certification/realName/index?type=1'
							})
						})
					}
				})
			}
		}
	})
}

//用户协议和隐私政策
function agreement(type){
	if(type == 'user'){
		uni.navigateTo({
			url: '/pagesC/user/agreement'
		})
	}else{
		uni.navigateTo({
			url: '/pagesC/user/policy'
		})
	}
}

onLoad((option)=>{
	// if(uni.getStorageSync('token') && uni.getStorageSync('userInfo')){
	// 	uni.switchTab({
	// 		url: '/pages/index/index'
	// 	})
	// }else{
	// 	uni.removeStorageSync('token')
	// 	uni.removeStorageSync('userInfo')
	// }
})

</script>

<style lang="less" scoped>
.container{
	display: flex;
	flex-direction: column;
	align-items: center;
	height: 100vh;
	padding: 0 50rpx;
	.logo{
		width: 288rpx;
		height: 234rpx;
		margin: 350rpx 0 36rpx 0;
	}
	.title{
		font-size: 40rpx;
		font-weight: 600;
		line-height: 56rpx;
		text-align: center;
	}
	.login-btn{
		width: 100%;
		margin-top: 300rpx;
		background-color: #007aff;
		color: #fff;
	}
	.footer{
		position: absolute;
		bottom: 50rpx;
		font-size: 26rpx;
		color: #999;
	}

	.policy{
		margin-top: 60rpx;
		display: flex;
		align-items: center;
		.active{
			border: 1px solid #02529B;
		}
		.i-active{
			background-color: #005BAE;
		}
		.out-circle{
			width: 32rpx;
			height: 32rpx;
			display: flex;
			align-items: center;
			justify-content: center;
			border-radius: 50%;
			margin-right: 10rpx;
			border: 1px solid #ccc;
			.inner-c{
				width: 20rpx;
				height: 20rpx;
				border-radius: 50%;
			}
		}
		.label{
			font-size: 26rpx;
			color: #101010;
			text{
				color: #005BAE;
			}
		}
	}
}
</style>